package com.tiyo.system.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tiyo.common.annotation.Log;
import com.tiyo.common.constant.UserConstants;
import com.tiyo.common.core.domain.AjaxResult;
import com.tiyo.common.enums.BusinessType;
import com.tiyo.framework.web.controller.BaseController;
import com.tiyo.system.mapper.domain.SysConfig;
import com.tiyo.system.service.SysConfigService;

@RequestMapping("/system/config")
@Controller
public class SysConfigController extends BaseController {
	private String prefix = "system/config";
	@Autowired
	SysConfigService sysConfigService;

	@GetMapping()
	public String config() {
		return prefix + "/config";
	}

//    @Log(title = "参数管理", businessType = BusinessType.EXPORT)
//    @RequiresPermissions("system:config:export")
//    @PostMapping("/export")
//    @ResponseBody
//    public AjaxResult export(SysConfig config)
//    {
//        List<Config> list = sysConfigService.selectConfigList(config);
//        ExcelUtil<Config> util = new ExcelUtil<Config>(Config.class);
//        return util.exportExcel(list, "参数数据");
//    }

	/**
	 * 新增参数配置
	 */
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 新增保存参数配置
	 */
	@RequiresPermissions("system:conf222ig:add")
	@Log(title = "参数管理", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(@Validated SysConfig config) {
		if (UserConstants.CONFIG_KEY_NOT_UNIQUE.equals(sysConfigService.checkConfigKeyUnique(config))) {
			return error("新增参数'" + config.getConfigName() + "'失败，参数键名已存在");
		}
		sysConfigService.insertConfig(config);
		return success();
	}
	 /**
     * 查询参数配置列表
     */
    @RequiresPermissions("system:config:list")
    @PostMapping("/list")
    @ResponseBody
    public AjaxResult list(SysConfig config)
    {
        return AjaxResult.success(sysConfigService.page(config));
    }
	/**
	 * 修改参数配置
	 */
	@GetMapping("/edit/{configId}")
	public String edit(@PathVariable("configId") String configId, ModelMap mmap) {
		mmap.put("config", sysConfigService.selectConfigById(configId));
		return prefix + "/edit";
	}

	/**
	 * 修改保存参数配置
	 */
	@RequiresPermissions("system:config:edit")
	@Log(title = "参数管理", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(@Validated SysConfig config) {
		if (UserConstants.CONFIG_KEY_NOT_UNIQUE.equals(sysConfigService.checkConfigKeyUnique(config))) {
			return error("修改参数'" + config.getConfigName() + "'失败，参数键名已存在");
		}
		sysConfigService.updateConfig(config);
		return success();
	}

	/**
	 * 删除参数配置
	 */
	@RequiresPermissions("system:config:remove")
	@Log(title = "参数管理", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		sysConfigService.deleteConfigByIds(ids);
		return success();
	}

	/**
	 * 清空缓存
	 */
	@RequiresPermissions("system:config:remove")
	@Log(title = "参数管理", businessType = BusinessType.CLEAN)
	@GetMapping("/clearCache")
	@ResponseBody
	public AjaxResult clearCache() {
		sysConfigService.clearCache();
		return success();
	}

	/**
	 * 校验参数键名
	 */
	@PostMapping("/checkConfigKeyUnique")
	@ResponseBody
	public String checkConfigKeyUnique(SysConfig config) {
		return sysConfigService.checkConfigKeyUnique(config);
	}
}
